﻿using Domain.Entity;
using DtoFw;
using DtoFw.Query;
using Repository;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Util;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SqlSugar;
using DtoFw.Enum;
using DtoFw.Common.Query;
using DtoFw.Statistics.Dto;
using DtoFw.Common.Dto;
using System.Net.NetworkInformation;

namespace Service
{
    public class StatisticsService
    {
        private readonly CaseRepository _caseRepository;
        private readonly MainOrganRepository _organRepository;
        public StatisticsService(CaseRepository caseRepository, MainOrganRepository organRepository)
        {
            this._caseRepository = caseRepository;
            this._organRepository = organRepository;
        }

        /// <summary>
        /// 查询单位树状对象
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<OrganTreeDto> GetOrganTree(CommonQuery query)
        {
            return BaseGenericResult<OrganTreeDto>.Succeed(_organRepository.GetAllOrgan(query));
        }

        /// <summary>
        /// 物品状态统计
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<EvidenceStatusDto>> StatisticsEvidenceStatus(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            //查询当前选中单位的下级以及自己ID
            List<MainOrganEntity> orgList = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => (x.SuperiorId == orgid || x.Id == orgid) && !x.IsDeleted).ToList();

            var linq = _caseRepository.GetDb()
                .Queryable<CaseEntity, CasePhysicalEvidenceEntity, CasePhysicalEvidenceTransfereEntity>
                ((t1, t2, t3) => new JoinQueryInfos(
                   JoinType.Left, t1.Id == t2.CaseId, JoinType.Left, t2.Id == t3.EvidenceId && t3.HistoryStatus == 0))
                  .Where((t1, t2, t3) => !t1.IsDeleted && !t2.IsDeleted)
                  //单位ID
                  .WhereIF(orgList.Any() && query.OrganType == 1, (t1, t2, t3) => orgList.Select(v => v.Id).Contains(t1.OrganId))
                  .WhereIF(orgList.Any() && query.OrganType == 2, (t1, t2, t3) => orgList.Select(v => v.Id).Contains(t1.HostUserOrgan.Value))
                  //如果是登记时间
                  .WhereIF(query.CType == 1 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2, t3) => t2.CreateTime >= query.CTimeStart && t2.CreateTime < query.CTimeEnd.Value.AddDays(1))
                  //如果是提取时间
                  .WhereIF(query.CType == 2 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2, t3) => t2.ExtractionDateTime >= query.CTimeStart && t2.ExtractionDateTime < query.CTimeEnd.Value.AddDays(1))

                  .Select((t1, t2, t3) => new EvidenceStatusDto()
                  {
                      OrganId = (query.OrganType == 1 ? t1.OrganId : (t1.HostUserOrgan ?? 0)),
                      OrganName = (query.OrganType == 1 ? t1.OrganName : t1.HostUserOrganName),
                      Status = t3.DeliveryDicType,
                      CStatus=t2.Status,
                  }).ToList();

            List<EvidenceStatusDto> list = new List<EvidenceStatusDto>();
            foreach (var item in orgList)
            {
                //其他出库类型  // 借调 1  检验 2  发还 3  销毁 4 移交 5 回库 6  更换位置7  入库8
                var issued = linq.Count(x => x.Status.HasValue && x.Status > 8 && x.OrganId == item.Id);
                var cin = linq.Count(x => (x.Status == 8 || x.Status == 7 || x.Status == 6) && x.OrganId == item.Id);
                var all = linq.Count(x => x.OrganId == item.Id);

                int register = linq.Count(x => !x.Status.HasValue && x.OrganId == item.Id);
                list.Add(new EvidenceStatusDto
                {
                    OrganId = item.Id,
                    OrganName = item.Name,
                    Register = register,
                    Warehoused = cin,
                    Seconded = linq.Count(x => x.Status == 1 && x.OrganId == item.Id),
                    Checked = linq.Count(x => x.Status == 2 && x.OrganId == item.Id),
                    Returned = linq.Count(x => x.Status == 3 && x.OrganId == item.Id),
                    Destroyed = linq.Count(x => x.Status == 4 && x.OrganId == item.Id),
                    HandedOver = linq.Count(x => x.Status == 5 && x.OrganId == item.Id),
                    Back = linq.Count(x => x.Status == 6 && x.OrganId == item.Id),
                    Issued = issued,
                    InAll = cin,
                    OutAll = all - cin - register,
                });
            }

            return BaseGenericResult<List<EvidenceStatusDto>>.Succeed(list);
        }

        /// <summary>
        /// 案件数量统计
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<EvidenceStatusDto>> StatisticsCase(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            //查询当前选中单位的下级以及自己ID
            List<MainOrganEntity> orgList = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => (x.SuperiorId == orgid || x.Id == orgid) && !x.IsDeleted).ToList();

            var linq = _caseRepository.GetDb()
                .Queryable<CaseEntity>()
                  .Where((t1) => !t1.IsDeleted)
                  //单位ID
                  .WhereIF(orgList.Any() && query.OrganType == 1, (t1) => orgList.Select(v => v.Id).Contains(t1.OrganId))
                  .WhereIF(orgList.Any() && query.OrganType == 2, (t1) => orgList.Select(v => v.Id).Contains(t1.HostUserOrgan.Value))
                  //如果是登记时间
                  .WhereIF(query.CType == 1 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1) => t1.CreateTime >= query.CTimeStart && t1.CreateTime < query.CTimeEnd.Value.AddDays(1))
                  //如果是案发时间
                  .WhereIF(query.CType == 2 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1) => t1.CaseTime >= query.CTimeStart && t1.CaseTime < query.CTimeEnd.Value.AddDays(1))

                  .Select((t1) => new EvidenceStatusDto()
                  {
                      OrganId = (query.OrganType == 1 ? t1.OrganId : (t1.HostUserOrgan ?? 0)),
                      OrganName = (query.OrganType == 1 ? t1.OrganName : t1.HostUserOrganName),
                  });

            List<EvidenceStatusDto> list = new List<EvidenceStatusDto>();
            foreach (var item in orgList)
            {
                list.Add(new EvidenceStatusDto
                {
                    OrganId = item.Id,
                    OrganName = item.Name,
                    CaseCount = linq.Count(x => x.OrganId == item.Id),
                });
            }

            return BaseGenericResult<List<EvidenceStatusDto>>.Succeed(list);
        }

        /// <summary>
        /// 物品数量统计
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<EvidenceStatusDto>> StatisticsEvidence(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            //查询当前选中单位的下级以及自己ID
            List<MainOrganEntity> orgList = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => (x.SuperiorId == orgid || x.Id == orgid) && !x.IsDeleted).ToList();

            var entityList = _caseRepository.GetDb()
                .Queryable<CaseEntity, CasePhysicalEvidenceEntity, CasePhysicalEvidenceTransfereEntity>
                ((t1, t2, t3) => new JoinQueryInfos(
                   JoinType.Left, t1.Id == t2.CaseId, JoinType.Left, t2.Id == t3.EvidenceId))
                  .Where((t1, t2, t3) => !t1.IsDeleted && !t2.IsDeleted)
                  //单位ID
                  .WhereIF(orgList.Any() && query.OrganType == 1, (t1, t2, t3) => orgList.Select(v => v.Id).Contains(t1.OrganId))
                  .WhereIF(orgList.Any() && query.OrganType == 2, (t1, t2, t3) => orgList.Select(v => v.Id).Contains(t1.HostUserOrgan.Value))
                  //如果是登记时间
                  .WhereIF(query.CType == 1 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2, t3) => t2.CreateTime >= query.CTimeStart && t2.CreateTime < query.CTimeEnd.Value.AddDays(1))
                  //如果是提取时间
                  .WhereIF(query.CType == 2 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2, t3) => t2.ExtractionDateTime >= query.CTimeStart && t2.ExtractionDateTime < query.CTimeEnd.Value.AddDays(1))

                  .Select((t1, t2, t3) => new EvidenceStatusDto()
                  {
                      OrganId = (query.OrganType == 1 ? t1.OrganId : (t1.HostUserOrgan ?? 0)),
                      OrganName = (query.OrganType == 1 ? t1.OrganName : t1.HostUserOrganName),
                      Status = t3.DeliveryDicType,
                  }).ToList();

            List<EvidenceStatusDto> list = new List<EvidenceStatusDto>();
            foreach (var item in orgList)
            {
                //其他出库类型  // 借调 1  检验 2  发还 3  销毁 4 移交 5 回库 6  更换位置7  入库8
                var cin = entityList.Count(x => (x.Status == 8 || x.Status == 7 || x.Status == 6) && x.OrganId == item.Id);
                var all = entityList.Count(x => x.OrganId == item.Id);

                list.Add(new EvidenceStatusDto
                {
                    OrganId = item.Id,
                    OrganName = item.Name,
                    Register = all,
                    InAll = cin,
                    OutAll = all - cin,
                });
            }

            return BaseGenericResult<List<EvidenceStatusDto>>.Succeed(list);
        }

        /// <summary>
        /// 库区数量统计
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<StatisticsRoomDto>> StatisticsRoom(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            // 查询当前选中单位
            MainOrganEntity organ = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => x.Id == orgid).First();

            List<RoomCabinetEntity> cabList = _caseRepository.GetDb().Queryable<RoomCabinetEntity>()
                .Where(x => x.OrganId == orgid && !x.IsDeleted)
                .WhereIF(query.roomId > 0, (t1) => t1.AreaId == query.roomId)
                .ToList();

            var entityList = _caseRepository.GetDb()
                .Queryable<RoomCabinetEntity, RoomBoxEntity, CasePhysicalEvidenceTransfereEntity, CasePhysicalEvidenceEntity, CaseEntity>
                ((t1, t2, t3, t4, t5) => new JoinQueryInfos(
                   JoinType.Left, t1.Id == t2.CabinetId, JoinType.Left, t2.Id == t3.LocationId, JoinType.Left, t3.EvidenceId == t4.Id, JoinType.Left, t4.CaseId == t5.Id))
                  .Where((t1, t2, t3, t4, t5) => !t1.IsDeleted && !t2.IsDeleted && !t3.IsDeleted && !t4.IsDeleted && !t5.IsDeleted)
                  //单位ID
                  .WhereIF(orgid > 0, (t1, t2, t3, t4, t5) => t1.OrganId == orgid)
                  //位置ID必须存在的数据
                  .Where((t1, t2, t3) => t3.LocationId > 0)
                  //区域id
                  .WhereIF(query.roomId > 0, (t1, t2, t3, t4, t5) => t1.AreaId == query.roomId)

                  .Select((t1, t2, t3, t4, t5) => new StatisticsRoomDto()
                  {
                      OrganId = organ.Id,
                      OrganName = organ.Name,
                      Id = t1.Id,
                      Name = t1.Name,
                      CabinetName = t1.CabinetName,
                      LocationId = t3.LocationId,
                      EvidenceId = t3.EvidenceId,
                      CaseId = t4.CaseId,
                  }).ToList();

            List<StatisticsRoomDto> list = new List<StatisticsRoomDto>();
            foreach (var item in cabList)
            {
                list.Add(new StatisticsRoomDto
                {
                    OrganId = orgid,
                    OrganName = query.OrganName,
                    Name = item.Name,
                    EvidenceCount = entityList.Where(v => v.Id == item.Id).Select(x => x.EvidenceId).Distinct().Count(),
                    CaseCount = entityList.Where(v => v.Id == item.Id).Select(x => x.CaseId).Distinct().Count(),
                });
            }

            return BaseGenericResult<List<StatisticsRoomDto>>.Succeed(list);
        }

        /// <summary>
        /// 区域下拉框数据
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<AreaDto>> StatisticsAreaDic(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            var entity = _caseRepository.GetDb().Queryable<RoomAreaEntity>().Where(x => x.OrganId == orgid && !x.IsDeleted).ToList();

            return BaseGenericResult<List<AreaDto>>.Succeed(AutoMapperExtension.MapTo<RoomAreaEntity, AreaDto>(entity));
        }

        /// <summary>
        /// 物品状态统计/实战分析
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<EvidenceStatusDto>> AnalysisEvidenceStatus(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            //查询当前选中单位的下级以及自己ID
            List<MainOrganEntity> orgList = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => (x.SuperiorId == orgid || x.Id == orgid) && !x.IsDeleted).ToList();

            var linq = _caseRepository.GetDb()
                .Queryable<CaseEntity, CasePhysicalEvidenceEntity>
                ((t1, t2) => new JoinQueryInfos(JoinType.Left, t1.Id == t2.CaseId)).Where((t1, t2) => !t1.IsDeleted && !t2.IsDeleted)

                  //单位ID
                  .WhereIF(orgList.Any() && query.OrganType == 1, (t1) => orgList.Select(v => v.Id).Contains(t1.OrganId))
                  .WhereIF(orgList.Any() && query.OrganType == 2, (t1) => orgList.Select(v => v.Id).Contains(t1.HostUserOrgan.Value))

                  //如果是登记时间
                  .WhereIF(query.CType == 1 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.CreateTime >= query.CTimeStart && t2.CreateTime < query.CTimeEnd.Value.AddDays(1))
                  //如果是提取时间
                  .WhereIF(query.CType == 2 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.ExtractionDateTime >= query.CTimeStart && t2.ExtractionDateTime < query.CTimeEnd.Value.AddDays(1))

                  .Select((t1, t2) => new EvidenceStatusDto()
                  {
                      OrganId = (query.OrganType == 1 ? t1.OrganId : (t1.HostUserOrgan ?? 0)),
                      OrganName = (query.OrganType == 1 ? t1.OrganName : t1.HostUserOrganName),
                      Status = t2.Status,
                  }).ToList();

            List<EvidenceStatusDto> list = new List<EvidenceStatusDto>();
            foreach (var item in orgList)
            {
                list.Add(new EvidenceStatusDto
                {
                    OrganId = item.Id,
                    OrganName = item.Name,
                    Register = linq.Count(v => v.OrganId == item.Id && v.Status == 0),
                    InAll = linq.Count(v => v.OrganId == item.Id && v.Status == 1),
                    OutAll = linq.Count(v => v.OrganId == item.Id && v.Status == 2),
                    AllCount = linq.Count(v => v.OrganId == item.Id)
                }); ;
            }

            return BaseGenericResult<List<EvidenceStatusDto>>.Succeed(list);
        }

        /// <summary>
        /// 物品类别统计/实战分析
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<EvidenceStatusDto>> AnalysisEvidenceClass(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            // 查询当前选中单位
            // MainOrganEntity organ = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => x.Id == orgid).First();

            var linq = _caseRepository.GetDb()
                  .Queryable<CaseEntity, CasePhysicalEvidenceEntity>
                   ((t1, t2) => new JoinQueryInfos(JoinType.Left, t1.Id == t2.CaseId)).Where((t1, t2) => !t1.IsDeleted && !t2.IsDeleted)

                  .Where((t1, t2) => !string.IsNullOrWhiteSpace(t2.ClassName))
                  //单位ID
                  .WhereIF(orgid > 0 && query.OrganType == 1, (t1) => t1.OrganId == orgid)
                  .WhereIF(orgid > 0 && query.OrganType == 2, (t1) => t1.HostUserOrgan == orgid)
                  .WhereIF(query.CType == 1 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.CreateTime >= query.CTimeStart && t2.CreateTime <= query.CTimeEnd.Value.AddDays(1))
                  //如果是提取时间
                  .WhereIF(query.CType == 2 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.ExtractionDateTime >= query.CTimeStart && t2.ExtractionDateTime <= query.CTimeEnd.Value.AddDays(1))

                  .Select((t1, t2) => new EvidenceStatusDto()
                  {
                      // OrganId = (query.OrganType == 1 ? t1.OrganId : (t1.HostUserOrgan ?? 0)),
                      // OrganName = (query.OrganType == 1 ? t1.OrganName : t1.HostUserOrganName),
                      Class = t2.Class,
                      ClassName = t2.ClassName,

                  }).ToList();

            List<EvidenceStatusDto> list = new List<EvidenceStatusDto>();
            foreach (var item in linq.Select(v => v.ClassName).ToList().Distinct())
            {
                list.Add(new EvidenceStatusDto
                {
                    //OrganId = organ.Id,
                    //OrganName = organ.Name,
                    Class = item,
                    ClassName = item,
                    ClassCount = linq.Count(v => v.ClassName == item),
                });
            }

            return BaseGenericResult<List<EvidenceStatusDto>>.Succeed(list.OrderByDescending(x => x.ClassCount).ToList());
        }

        /// <summary>
        /// 物品来源统计/实战分析
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<EvidenceStatusDto>> AnalysisEvidenceSource(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            // 查询当前选中单位
            MainOrganEntity organ = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => x.Id == orgid).First();

            var linq = _caseRepository.GetDb()
                 .Queryable<CaseEntity, CasePhysicalEvidenceEntity>
                ((t1, t2) => new JoinQueryInfos(JoinType.Left, t1.Id == t2.CaseId)).Where((t1, t2) => !t1.IsDeleted && !t2.IsDeleted)

                  .Where((t1, t2) => !string.IsNullOrWhiteSpace(t2.ExtractMethod))
               //单位ID
                  .WhereIF(orgid > 0 && query.OrganType == 1, (t1) => t1.OrganId == orgid)
                  .WhereIF(orgid > 0 && query.OrganType == 2, (t1) => t1.HostUserOrgan == orgid)
                  //如果是登记时间
                  .WhereIF(query.CType == 1 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.CreateTime >= query.CTimeStart && t2.CreateTime < query.CTimeEnd.Value.AddDays(1))
                  //如果是提取时间
                  .WhereIF(query.CType == 2 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.ExtractionDateTime >= query.CTimeStart && t2.ExtractionDateTime < query.CTimeEnd.Value.AddDays(1))

                  .Select((t1, t2) => new EvidenceStatusDto()
                  {
                      OrganId = (query.OrganType == 1 ? t1.OrganId : (t1.HostUserOrgan ?? 0)),
                      OrganName = (query.OrganType == 1 ? t1.OrganName : t1.HostUserOrganName),
                      SName = t2.ExtractMethod,

                  }).ToList();

            List<EvidenceStatusDto> list = new List<EvidenceStatusDto>();
            foreach (var item in linq.Select(v => v.SName).ToList().Distinct())
            {
                list.Add(new EvidenceStatusDto
                {
                    OrganId = organ.Id,
                    OrganName = organ.Name,
                    ClassName = item,
                    ClassCount = linq.Count(v => v.SName == item),
                });
            }

            return BaseGenericResult<List<EvidenceStatusDto>>.Succeed(list.OrderByDescending(x => x.ClassCount).ToList());
        }

        /// <summary>
        /// 提取部位统计/实战分析
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<EvidenceStatusDto>> AnalysisExtractionParts(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            // 查询当前选中单位
            MainOrganEntity organ = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => x.Id == orgid).First();

            var entityList = _caseRepository.GetDb()
                 .Queryable<CaseEntity, CasePhysicalEvidenceEntity>
                ((t1, t2) => new JoinQueryInfos(JoinType.Left, t1.Id == t2.CaseId)).Where((t1, t2) => !t1.IsDeleted && !t2.IsDeleted)
                  .Where((t1, t2) => !t1.IsDeleted && !string.IsNullOrWhiteSpace(t2.ExtractionParts))
                   //单位ID
                   .WhereIF(orgid > 0 && query.OrganType == 1, (t1) => t1.OrganId == orgid)
                  .WhereIF(orgid > 0 && query.OrganType == 2, (t1) => t1.HostUserOrgan == orgid)
                  //如果是登记时间
                  .WhereIF(query.CType == 1 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.CreateTime >= query.CTimeStart && t2.CreateTime < query.CTimeEnd.Value.AddDays(1))
                  //如果是提取时间
                  .WhereIF(query.CType == 2 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.ExtractionDateTime >= query.CTimeStart && t2.ExtractionDateTime < query.CTimeEnd.Value.AddDays(1))

                  .Select((t1, t2) => new EvidenceStatusDto()
                  {
                      OrganId = (query.OrganType == 1 ? t1.OrganId : (t1.HostUserOrgan ?? 0)),
                      OrganName = (query.OrganType == 1 ? t1.OrganName : t1.HostUserOrganName),
                      SName = t2.ExtractionParts,

                  }).ToList();

            List<EvidenceStatusDto> list = new List<EvidenceStatusDto>();
            foreach (var item in entityList.Select(v => v.SName).ToList().Distinct())
            {
                list.Add(new EvidenceStatusDto
                {
                    OrganId = organ.Id,
                    OrganName = organ.Name,
                    ClassName = item,
                    ClassCount = entityList.Count(v => v.SName == item),
                });
            }

            return BaseGenericResult<List<EvidenceStatusDto>>.Succeed(list.OrderByDescending(x => x.ClassCount).ToList());
        }

        /// <summary>
        /// 提取方法统计/实战分析
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<EvidenceStatusDto>> AnalysisExtractMethod(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            // 查询当前选中单位
            MainOrganEntity organ = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => x.Id == orgid).First();

            var entityList = _caseRepository.GetDb()
                  .Queryable<CaseEntity, CasePhysicalEvidenceEntity>
                 ((t1, t2) => new JoinQueryInfos(JoinType.Left, t1.Id == t2.CaseId)).Where((t1, t2) => !t1.IsDeleted && !t2.IsDeleted)

                  .Where((t1, t2) => !t1.IsDeleted && !string.IsNullOrWhiteSpace(t2.ExtractMethod))
                //单位ID
                .WhereIF(orgid > 0 && query.OrganType == 1, (t1) => t1.OrganId == orgid)
                  .WhereIF(orgid > 0 && query.OrganType == 2, (t1) => t1.HostUserOrgan == orgid)
                  //如果是登记时间
                  .WhereIF(query.CType == 1 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.CreateTime >= query.CTimeStart && t2.CreateTime < query.CTimeEnd.Value.AddDays(1))
                  //如果是提取时间
                  .WhereIF(query.CType == 2 && query.CTimeStart.HasValue && query.CTimeEnd.HasValue,
                  (t1, t2) => t2.ExtractionDateTime >= query.CTimeStart && t2.ExtractionDateTime < query.CTimeEnd.Value.AddDays(1))

                  .Select((t1, t2) => new EvidenceStatusDto()
                  {
                      OrganId = (query.OrganType == 1 ? t1.OrganId : (t1.HostUserOrgan ?? 0)),
                      OrganName = (query.OrganType == 1 ? t1.OrganName : t1.HostUserOrganName),
                      SName = t2.ExtractMethod,

                  }).ToList();

            List<EvidenceStatusDto> list = new List<EvidenceStatusDto>();
            foreach (var item in entityList.Select(v => v.SName).ToList().Distinct())
            {
                list.Add(new EvidenceStatusDto
                {
                    OrganId = organ.Id,
                    OrganName = organ.Name,
                    ClassName = item,
                    ClassCount = entityList.Count(v => v.SName == item),
                });
            }

            return BaseGenericResult<List<EvidenceStatusDto>>.Succeed(list.OrderByDescending(x => x.ClassCount).ToList());
        }

        /// <summary>
        /// 物品库房统计/实战分析
        /// </summary>
        /// <returns></returns>
        public BaseGenericResult<List<StatisticsRoomDto>> AnalysisRoom(CommonQuery query)
        {
            long orgid = query.CurrentOrganId;
            if (orgid <= 0)
            {
                orgid = query.OrganId;
            }

            // 查询当前选中单位
            MainOrganEntity organ = _caseRepository.GetDb().Queryable<MainOrganEntity>().Where(x => x.Id == orgid).First();

            List<RoomAreaEntity> areaList = _caseRepository.GetDb().Queryable<RoomAreaEntity>()
              .Where(x => x.OrganId == orgid && !x.IsDeleted)
              .OrderBy(v => v.OrderBy)
              .ToList();


            var entityList = _caseRepository.GetDb()
                   .Queryable<RoomAreaEntity, RoomCabinetEntity, RoomBoxEntity>
                ((t1, t2, t3) => new JoinQueryInfos(
                   JoinType.Left, t1.Id == t2.AreaId, JoinType.Left, t2.Id == t3.CabinetId))
                  .Where((t1, t2, t3) => !t1.IsDeleted && !t2.IsDeleted && !t3.IsDeleted)
                  //单位ID
                  .WhereIF(orgid > 0, (t1) => t1.OrganId == orgid)
                  .OrderBy((t1) => t1.OrderBy)
                  .Select((t1, t2, t3) => new StatisticsRoomDto()
                  {

                      AreaName = t1.Name,
                      Aid = t1.Id,
                      Cid = t2.Id,
                      Ctype = t2.Smart,
                      Bid = t3.Id,
                  }).ToList();

            List<StatisticsRoomDto> list = new List<StatisticsRoomDto>();
            foreach (var item in areaList)
            {
                list.Add(new StatisticsRoomDto
                {
                    OrganId = organ.Id,
                    OrganName = organ.Name,
                    AreaName = item.Name,
                    AllCabinetCount = entityList.Where(v => v.Aid == item.Id).Select(x => x.Cid).Distinct().Count(),
                    CleverCabinetCount = entityList.Where(v => v.Aid == item.Id && v.Ctype == true).Select(x => x.Cid).Distinct().Count(),
                    CabinetCount = entityList.Where(v => v.Aid == item.Id && v.Ctype == false).Select(x => x.Cid).Distinct().Count(),
                    BoxCount = entityList.Where(v => v.Aid == item.Id).Select(x => x.Bid).Distinct().Count(),
                });
            }

            return BaseGenericResult<List<StatisticsRoomDto>>.Succeed(list);
        }

    }
}
